<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>copy_backward</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Algorithms</a> &gt; <a href=
    "copy_backward.html">copy_backward</a>
  </div>

  <div class="name-format">
    copy_backward
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;algorithm&gt;
  iterator copy_backward( iterator start, iterator end, iterator dest );
</pre>

  <p>copy_backward() is similar to (C++ Strings) <a href=
  "../cppstring/copy.html">copy</a>(), in that both functions copy
  elements from <em>start</em> to <em>end</em> to <em>dest</em>. The
  copy_backward() function , however, starts depositing elements at
  <em>dest</em> and then works backwards, such that:</p>
  <pre class="example-code">
 *(dest-1) == *(end-1)
 *(dest-2) == *(end-2)
 *(dest-3) == *(end-3)
 ...
 *(dest-N) == *(end-N)          
</pre>

  <p>The following code uses copy_backward() to copy 10 integers into
  the end of an empty vector:</p>
  <pre class="example-code">
 vector&lt;int&gt; from_vector;
 for( int i = 0; i &lt; 10; i++ ) {
   from_vector.push_back( i );
 }              

 vector&lt;int&gt; to_vector(15);               

 copy_backward( from_vector.begin(), from_vector.end(), to_vector.end() );              

 cout &lt;&lt; &quot;to_vector contains: &quot;;
 for( unsigned int i = 0; i &lt; to_vector.size(); i++ ) {
   cout &lt;&lt; to_vector[i] &lt;&lt; &quot; &quot;;
 }
 cout &lt;&lt; endl;            
</pre>

  <p>The above code produces the following output:</p>
  <pre class="example-code">
 to_vector contains: 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9              
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="copy.html">copy</a><br>
    <a href="copy_n.html">copy_n</a><br>
    <a href="swap.html">swap</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
